<?php

class Admin_Form_Horariosagendamentonap extends Zend_Form
{
    public function init() {
   	    
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        $urlbase = $front->getBaseUrl();
        
        $id = $front->getRequest()->getParam('id', 0);

        $this->setName('form_pdr');
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        Zend_Loader::loadClass('datetools','library');
        
        $el = new Zend_Form_Element_Hidden('HAG_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
       
        $arr = array(''=> 'Nenhum');
        $query = 'SELECT SQL_CACHE USU_ID,USU_NOME FROM USUARIOS WHERE PER_ID = 8 ORDER BY USU_NOME';
        $result = $db->fetchAll($query);
        foreach($result as $row) $arr[$row['USU_ID']] = $row['USU_NOME'];
        
        $el = new Zend_Form_Element_Select('USU_ID',array('onchange' => ''));
        $el->setLabel('Profissional')->setRequired(true);
        $el->addMultiOptions($arr);
        $el->setAttrib('required','');
        $this->addElement($el);
        
        $html_row = '';
        if($id > 0){
            $query = 'SELECT SQL_CACHE HAH_HORINI,HAH_HORFIM,HAH_DIASEM FROM HORARIOS_AGENDAMENTONAP_HORA WHERE HAG_ID = '.$id;
            $result = $db->fetchAll($query);
            foreach($result as $row){
                
                $arr = Datetools::diaSemana();
                $html = '<select name="HAH_DIASEM[]" class="tam_med" ><option value="">Nenhum</option>';
                foreach($arr as $key => $dia){
                    $selected = $row['HAH_DIASEM'] == $key ? 'selected':'';
                    $html .= '<option '.$selected.' value="'.$key.'">'.$dia.'</option>';
                }
                $html .= '</select>';
                $cmb_itg = $html;
                
                $html_row .= '<div class="cnt_el">';
                $html_row .= ' Dia: ' . $cmb_itg;
                $html_row .= ' &nbsp; Hora Inicial: <input type="text" class="hora" name="HAH_HORINI[]" value="'.$row['HAH_HORINI'].'" >';
                $html_row .= ' &nbsp; Hora Final: <input type="text" class="hora" name="HAH_HORFIM[]" value="'.$row['HAH_HORFIM'].'" >';
                $html_row .= ' &nbsp; <a href="javascript:void(0)" class="excluir">Excluir</a>';
                $html_row .= '</div>'; 
            }
        }
        
        $arr = Datetools::diaSemana();
        $html = '<select name="HAH_DIASEM[]" class="tam_med" ><option value="">Nenhum</option>';
        foreach($arr as $key => $dia){
            $html .= '<option value="'.$key.'">'.$dia.'</option>';
        }
        $html .= '</select>';
        $cmb_itg = $html;
        
        $html2 .= '<div class="cnt_el">';
        $html2 .= ' Dia: ' . $cmb_itg;
        $html2 .= ' &nbsp; Hora Inicial: <input type="text" class="hora" name="HAH_HORINI[]" >';
        $html2 .= ' &nbsp; Hora Final: <input type="text" class="hora" name="HAH_HORFIM[]" >';
        $html2 .= ' &nbsp; <a href="javascript:void(0)" class="excluir">Excluir</a>';
        $html2 .= '</div>'; 
        
        $html = '<div id="it_dis">';
        $html .= '<div class="cnt_el"><input type="button" name="add" id="add" value="Adicionar Horário" class="btn-frm"></div>';
        $html .= $html_row;
        if($html_row == '') $html .= $html2;
        $html .= '</div>';
        
        $el = new Zend_Form_Element('html',array( 'helper'=>'formNote', 'value'=> $html ));
        $this->addElement($el);
        
        if($id > 0){
            $el = new Zend_Form_Element_Checkbox('HAG_STA',array('checked'=>'checked'));
            $el->setLabel('Status');
            $this->addElement($el);
        }
        
        $btn = new Zend_Form_Element_Submit('Salvar',array('disableLoadDefaultDecorators' =>true));
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setLabel('Salvar');
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-frm">Voltar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-frm');
        $this->addElement($btn);
        
        
    }

}

